package com.sys.wares.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import com.sys.common.pojo.ApiResponse;
import com.sys.common.pojo.ApiResponseEnum;
import com.sys.common.pojo.JSONEnum;
import com.sys.common.service.BaseServiceImpl;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.wares.dao.ITCateDao;
import com.sys.wares.dao.ITSuggDao;
import com.sys.wares.dao.ITWaresDao;
import com.sys.wares.dao.impl.AWaresDaoImpl;
import com.sys.wares.dao.impl.TCateDaoImpl;
import com.sys.wares.dao.impl.TSuggDaoImpl;
import com.sys.wares.pojo.AWares;
import com.sys.wares.pojo.TCate;
import com.sys.wares.pojo.TSugg;
import com.sys.wares.pojo.TSuggEnum;
import com.sys.wares.service.IGoodsDbService;

/**
 * Service实现类(商品代码)
 * 
 * @author 杨志超
 */
public class GoodsDbServiceImpl extends BaseServiceImpl implements IGoodsDbService
{
	private ITWaresDao goodsDao = new AWaresDaoImpl();
	@Resource
	private ITCateDao cateDao = new TCateDaoImpl();
	private ITSuggDao commentDao = new TSuggDaoImpl();
	
	@Override
	public ApiResponse<Object> saveOneGoodsService(AWares goods)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		goods.setCreateTime(new Date());
		goods.setUpdateTime(new Date());
		int res = this.goodsDao.save(goods);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), goods.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), goods);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneGoodsService(AWares goods)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		goods.setUpdateTime(new Date());
		int res = this.goodsDao.update(goods);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), goods.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), goods);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<Object> updateOneGoodsService(AWares goods, Map<String, Object> paramsMap)
	{
		/* 先更新用户的信息 */
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		try
		{
			String fileName = paramsMap.get("fileName") + "";
			InputStream is = (InputStream) paramsMap.get("is") ;
			
			/* 上传文件的相对路径   /uploadfile/  */
			String uploadFile = ConstatFinalUtil.CONFIG_JSON.getString("website.uploadFile");
			/* 存储的是网址的绝对路径 */ 
			String souPath = goods.getListImg();
			/* 真实路径  D:/项目空间/usersCenter/usersCenter-web/usersCenter-web-head/src/main/webapp/ */
			String truePath = ConstatFinalUtil.CONFIG_JSON.getString("website.truePath");
			/* 网站的相对路径 */
			String tarRelaPath = uploadFile + "goods/" + this.dateFormatUtil.dateStr(new Date()) + "/" + UUID.randomUUID().toString() + 
					fileName.substring(fileName.lastIndexOf("."), fileName.length());
			File tarTrueFile = new File(truePath + tarRelaPath.substring(1));
			
			/* 创建父目录 */
			if(!tarTrueFile.getParentFile().exists())
			{
				tarTrueFile.getParentFile().mkdirs();
			}
			/* 设置路径 */
			goods.setListImg(tarRelaPath);
			FileOutputStream fos = new FileOutputStream(tarTrueFile);
			/* 复制文件 */
			boolean flag = this.fileUtil.copyFile(is, fos);
			if(flag)
			{
				if(souPath != null && !"".equalsIgnoreCase(souPath))
				{
					souPath=truePath+souPath;
					/* 相对路径 */
					File souFile = new File(souPath);
					if(!souFile.delete())
					{
						ConstatFinalUtil.SYS_LOGGER.error("删除原始文件失败:原始文件路径:{}",souFile.getAbsolutePath());
					}
				}
			}
			/* 更新用户信息 */
			apiResponse = this.updateOneGoodsService(goods);
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("上传头像失败了,id:{}",goods.getId() , e);
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneGoodsService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.goodsDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<AWares> findCondListGoodsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<AWares> apiResponse = new ApiResponse<AWares>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<AWares> demoGoodsList = Collections.EMPTY_LIST;

		Map<String, List<AWares>> dataList = new HashMap<String, List<AWares>>();

		demoGoodsList = this.goodsDao.findCondList(pageInfoUtil, condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			
			for (Iterator iterator = demoGoodsList.iterator(); iterator.hasNext();)
			{
				AWares goods = (AWares) iterator.next();
				
				extMap.clear();
				extMap.put("id", goods.getCateId());
				ApiResponse<TCate> cateApiResponse = this.findOneCateService(extMap);
				TCate cate = cateApiResponse.getDataOneJava();
				goods.setCate(cate);
				
			}
		}
		
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoGoodsList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoGoodsList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<AWares> findOneGoodsService(Map<String, Object> condMap)
	{
		ApiResponse<AWares> apiResponse = new ApiResponse<AWares>();

		Map<String, AWares> data = new HashMap<String, AWares>();
		AWares goods = this.goodsDao.findOne(condMap);
		
		if("true".equalsIgnoreCase(condMap.get("extend") + ""))
		{
			/* 查询关联关系 */
			Map<String, Object> extMap = new HashMap<String, Object>();
			extMap.clear();
			extMap.put("id", goods.getCateId());
			ApiResponse<TCate> cateApiResponse = this.findOneCateService(extMap);
			TCate cate = cateApiResponse.getDataOneJava();
			goods.setCate(cate);
		}
		
		data.put(ApiResponseEnum.NAME_ONE.getName(), goods);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(goods);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<Object> saveOneCateService(TCate cate)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		cate.setCreateTime(new Date());
		cate.setUpdateTime(new Date());
		int res = this.cateDao.save(cate);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), cate.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), cate);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneCateService(TCate cate)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		cate.setUpdateTime(new Date());
		int res = this.cateDao.update(cate);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), cate.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), cate);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneCateService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.cateDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<TCate> findCondListCateService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<TCate> apiResponse = new ApiResponse<TCate>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<TCate> demoCateList = Collections.EMPTY_LIST;

		Map<String, List<TCate>> dataList = new HashMap<String, List<TCate>>();
		demoCateList = this.cateDao.findCondList(pageInfoUtil, condMap);
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoCateList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoCateList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<TCate> findOneCateService(Map<String, Object> condMap)
	{
		ApiResponse<TCate> apiResponse = new ApiResponse<TCate>();

		Map<String, TCate> data = new HashMap<String, TCate>();
		TCate cate = this.cateDao.findOne(condMap);
		data.put(ApiResponseEnum.NAME_ONE.getName(), cate);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(cate);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<Object> saveOneCommentService(TSugg comment)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		comment.setStatus(TSuggEnum.STATUS_ENABLE.getStatus());

		comment.setCreateTime(new Date());
		comment.setUpdateTime(new Date());
		comment.setPubTime(new Date());
		int res = this.commentDao.save(comment);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), comment.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), comment);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> updateOneCommentService(TSugg comment)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		comment.setUpdateTime(new Date());
		int res = this.commentDao.update(comment);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), comment.getId());
			dataMap.put(ApiResponseEnum.NAME_ONE.getName(), comment);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> deleteOneCommentService(Map<String, Object> condMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int res = this.commentDao.delete(condMap);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			apiResponse.setDataOne(dataMap);

			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<TSugg> findCondListCommentService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		ApiResponse<TSugg> apiResponse = new ApiResponse<TSugg>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<TSugg> demoCateList = Collections.EMPTY_LIST;

		Map<String, List<TSugg>> dataList = new HashMap<String, List<TSugg>>();
		demoCateList = this.commentDao.findCondList(pageInfoUtil, condMap);
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoCateList);

		apiResponse.setPageInfoUtil(pageInfoUtil);
		apiResponse.setDataListJava(demoCateList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<TSugg> findOneCommentService(Map<String, Object> condMap)
	{
		ApiResponse<TSugg> apiResponse = new ApiResponse<TSugg>();

		Map<String, TSugg> data = new HashMap<String, TSugg>();
		TSugg comment = this.commentDao.findOne(condMap);
		data.put(ApiResponseEnum.NAME_ONE.getName(), comment);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(comment);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
}
